/**
* Common 使用 

  common.alert("警告信息",function(){
   //......
  });

common.success("警告信息",function(){
   //......
  });
  
common.error("警告信息",function(){
 //......
});  
common.frame("http://www.baidu.com","百度","800","600");

common.confirm("你确定要删除该信息吗?",function(result){
	if(result){
		alert("删除成功");
	}
});
//关闭
common.close()
*/
var Common=function Common(){};
//警告
Common.prototype.alert=function(message,callBack){
	jAlert(message,"警告",function(data){
	 		Common.MyCallBack(data,callBack);
	 });
};
/**
* 成功
*/
Common.prototype.success=function(message,callBack){
	jAlert(message,"成功",function(data){
	 		Common.MyCallBack(data,callBack);
	 });
};
/*
* 错误
*/
Common.prototype.error=function(message,callBack){
	jAlert(message,"错误",function(data){
	 		Common.MyCallBack(data,callBack);
	 });
};

/**
* 弹出新页面
*/
Common.prototype.frame=function(url,title,width,height){
	jIframe(url,title,width,height)
};
/**
* 确认框
*/
Common.prototype.confirm=function(message,callBack){
	jConfirm(message,"确认",function(data){
	 		Common.MyCallBack(data,callBack);
	 });
};
/** 删除前确认*/

Common.prototype.confirmDelete=function(message,url,param,callBack){
	 common.confirm(message,function(result){
		if(result){
			common.send(url,param||{},function(data){
				callBack(data);
			})
		}
	});
};


/**
* Prompt
*/
Common.prototype.prompt=function(message,title,value,callBack){
	jPrompt(message,title,value,function(data){
	 		Common.MyCallBack(data,callBack);
	 });
};
/**
* 提醒信息
*/
Common.prototype.message=function(message,callBack){
	jMessage(message,"提醒",callBack);
};

/**
 * html 片段 交互型
 */
Common.prototype.choose=function(html,title,width,height,callBack){
	jChoose(html,title,width,height,function(data){
	 		Common.MyCallBack(data,callBack);
	 });
}

/**
 * 
 * 
 关闭
*/
Common.prototype.close=function(){
$.alerts._hide();
};

/**
 关闭后新弹出提示信息
*/
Common.prototype.messageClose=function(message){
$.alerts._hide();
common.message(message);
};
/***
 打开新页签
*/
Common.prototype.openWin=function(url,title,id,closeable){
		//window.top.right.testRight($('<a  dataType="iframe" dataLink="'+url+'" isRefresh="'+refresh+'">'+title+'</a>'));
//	console.debug(typeof id !='undefined');
	//当id 为false的时候说明 没有传递id closeable 为false 需要专门处理一下。
	if(id==false){
		id=null;
		closeable=false;
	}
	if(typeof closeable=='undefined'){
		closeable=true;
	}
	if(typeof id !='undefined'){
		//console.debug(id);
		id ='tag_'+ id;
		if(id.length>0){
			window.top.mainFrame.addUrl(id,url,title,closeable);
		}
	}else{
		window.top.mainFrame.addUrl((new Date()).getTime(),url,title,closeable);
	}
};


Common.prototype.closeCurrentWin=function(){
	//window.top.right.removeSelectedTab();
	window.top.mainFrame.closeTab();
};

/**
* post发送信息
*/
Common.prototype.send=function(url,param,callBack){
	param=param||{};
	 Common.MyPost(url,param,function(data){
	 		Common.MyCallBack(data,callBack);
	 });
};

/*
* 将整个form表单动态提交
*/
Common.prototype.sendForm=function($form,callBack){
	var url=$form.attr("action");
	var param=$form.serialize();
	if($.messager){
		 $.messager.progress({
			title:'请稍等',
			msg:'数据处理中'
		});
	}
	 Common.MyPost(url,param,function(data){
	 		Common.MyCallBack(data,callBack);
	 });
};

/**
 	重新登录
*/
Common.prototype.login=function(title,callBack){
	var userAccount=$(".header",window.top.frames[0].document).attr("userAccount");
	var tenantCode=$(".header",window.top.frames[0].document).attr("tenantCode");
	var extNo=$(".header",window.top.frames[0].document).attr("extNo");
	var html='<form id="chooseForm"><table border="0" cellpadding="0" cellspacing="0">';
		html+='<tr><td  height="24" align="right">用户名:</td><td  height="24">'+userAccount+'</td></tr>';
		html+='<tr><td align="right">密码:</td><td><input id="password" type="password" name="password" datatype="n4-20" errormsg="密码不能够少于3位"/></td></tr>';
		html+='</table></form>';
	window.top.frames[3].common.choose(html,title,400,300,function(htm){
		if(htm){
			common.send(webRoot+"/sys/login.htm",{username:userAccount,password:$("#password",$(htm)).val(),product:tenantCode,extNo:extNo},function(data){
				callBack(data);
			});
		}
	});
};

/** 私有方法不要调用*/
Common.MyCallBack=function(data,callBack){
	if(data){
	 			if(data&&data.status&&data.status.code!='SUCCESS'){
	 				common.error(data.msg+": <span style='color:red'>"+(data.exception==null?"":""+data.exception)+"</span>");
	 			}else{
		 			if(callBack){
				 		callBack(data.result?data.result:data);
		 			}
	 		}
	 }else{
	 	callBack(data);
	 }
};
Common.MyPost=function(url,param,callBack){
	//$("#back",window.top.right.document).show();
	$.ajax( {
				"url":  url,
				"data": param,
				"success": function(data){
						if($.messager){
							$.messager.progress('close');
						}
						callBack(data);
						//$("#back",window.top.right.document).hide()
						},
				"dataType": "json",
				"cache": false,
				"type": "POST",
				"beforeSend":function(){
					
				},
				"complete":function(){
					
				},
				"error": function (xhr, error, thrown) {
					//console.log(error);
					//console.log(xhr);
					if(xhr.responseText.indexOf("frameset")!=-1){
						callBack({status:200,msg:"登录成功"});
					}else if(xhr.responseText.indexOf("登录页")!=-1){
						callBack({status:505,msg:"登录失败"});					
					}
					//$("#back",window.top.right.document).hide();
				}
			} );
};


Common.prototype.download=function(param){
	if(param["link"]){
		location=webRoot+""+param["link"];
	}else{
		
	}
}
Common.prototype.upload=function(uploadId,callBack){
	var file=$("#"+uploadId);
	var attachmentType=file.attr("attachmentType");
		attachmentType=typeof(attachmentType)=="undefined"?"":attachmentType;
		var refId=file.attr("refId");
		refId=typeof(refId)=="undefined"?"":refId;
	$.ajaxFileUpload({
   	 	url:webRoot+"/sys/attachment/addAttachment.json?attachmentType="+attachmentType+"&refId="+refId,  
        secureuri:false,
        fileElementId : uploadId,
        dataType: 'json',
        success: function (data, status){
        	if(data.status.code=="SUCCESS"){
	        	callBack(data.result);
        	}else{
        		common.error("上传失败!");
        	}
        },
        error: function (data, status, e){
          common.error("上传失败!");
        }
      })
};

Common.prototype.deleteAttachment=function(attachmentId,callBack){
	alert(attachmentId);
	if(callBack){callBack('')};
};


Common.prototype.toHTML=function(o){
	return $(o).text();
}
var common=new Common();

/**
	附件相关
	<button id="addAttachment" refId="${knowledgeBaseTemp.id}">添加附件</button>
	<div id="AttachmentDiv">
		<c:forEach var="attachment" items="${knowledgeBaseTemp.attachmentList}">
			<div>
				<span><a href="#" class="aHref" id="${attachment.id}">${attachment.attachmentName}</a></span><input type="button" class="deleteAttachment" value="删除"/>
			</div>
		</c:forEach>
	</div>
*/
$(function(){
	/**
		下载附件
	*/
	$(".aHref").on("click",function(){
		var self =$(this);
		var id=self.attr("id");
		location=webRoot+"/sys/attachment/download.htm?id="+id;
	});
	/**
		添加附件
	*/
	$("#addAttachment").click(function(){
		var refId=$(this).attr("refId");
		var div=$("#AttachmentDiv");
		if($("div",div).length>4){
			common.alert("附件最多不能超过5个");
		}
		var t=new Date().getTime();
			var htm="<div>"
			htm+="<input type='file' name='file' id='file"+t+"' attachmentType='knowledge' refId='"+refId+"'/>";
			htm+="<input type='button' value='删除' class='deleteAttachment'/>";
			htm+="</div>";
		div.append(htm);
		return false;
	});
		
	/**
		删除附件
	*/
	$(".deleteAttachment").on("click",function(){
		var self=$(this);
			var span=self.parent().find("span");
		if(span.length>0){
			common.confirm("你确定要删除该文件吗? 删除后不能回复。",function(result){
				if(result){
					common.send(webRoot+"/sys/attachment/deleteAttachment.json",{ids:span.find("a").attr("id")},function(data){
						common.messageClose("操作成功");
						self.parent().remove();
					});
				}
			})		
		}else{
			self.parent().remove();
		}
	});
});


//日期控件 My97Date重写
//eg: from:<input type="text"   id="dateFrom"  class="Wdate" maxId="dateTo"   format="yyyy-MM-dd" />    format="yyyy-MM-dd HH:mm:ss"
//	  to:<input type="text"    id="dateTo" class="Wdate"    minId="dateFrom" format="yyyy-MM-dd"/>
$(function () {
	$(".Wdate,.WdateTime").on("click", function () {
		var self = $(this);
		var s = "\tWdatePicker({dateFmt:self.attr(\"format\")";
		if (self.attr("maxDate")) {
			s += ",maxDate:$(this).attr(\"maxDate\")";
		}
		if (self.attr("minDate")) {
			s += ",minDate:$(this).attr(\"minDate\")";
		}
		if (self.attr("minId")) {
			s += ",minDate:\"" + $("#" + self.attr("minId")).val() + "\"";
		}
		if (self.attr("maxId")) {
			s += ",maxDate:\"" + $("#" + self.attr("maxId")).val() + "\"";
		}
		s+=",onpicked:function(dp){$('#'+dp.el.id).trigger('change')}";
		s += "})";
		return eval(s);
	});
	
	$(".closed_dialog").click(function(){
		parent.common.close();
	});
});


//字符串Tim方法
String.prototype.trim=function(){
	
	return this.replace(/(^\s*)|(\s*$)/g, "");
};



$(function(){	//屏蔽退格删除键  
	$(document).keydown(function(event){
	  if (event.which==8){    //屏蔽退格删除键   
		 if(!$(event.target).is(":input")){
			 event.preventDefault();
		 } 
	  }  
	});
});

